# -*- mode: python; -*-
import os
import sys
from tools import installer

# Defines option --compat
AddOption('--compat',
          dest='compat_option',
          action="store_true",
          help='Maintains compatibility with Keyczar 0.6b')



#### Creates a base environment to describe what is common between platforms.
base_env = Environment()
base_env.Tool('component_setup')
base_env.Append(
    # All includes are relative to the main project directory.
    CPPPATH = ['$MAIN_DIR'],

    # The list of components common to all platforms.
    BUILD_SCONSCRIPTS = ['testing', 'keyczar'],

    # Sets architecture byte order
    CPPDEFINES = [
        {'little': 'NWORDS_BIGENDIAN',
         'big': 'WORDS_BIGENDIAN'}[sys.byteorder]
        ],

    # Keyczar source directory
    KEYCZAR_SRC_DIR = os.path.join(os.getcwd(), 'keyczar'),
    )

if GetOption('compat_option'):
    base_env.Append(
        # Option to preserve compatibility with Keyczar Java
        CPPDEFINES = ['COMPAT_KEYCZAR_06B']
        )



#### Installer
# Read variables from file
opts = Variables('options.conf', ARGUMENTS)

# add the installer options
installer.AddOptions(opts)
opts.Update(base_env)
opts.Save('options.conf', base_env)
Help(opts.GenerateHelpText(base_env))



#### Checking dependancies

def CheckCommand(context, cmd):
    context.Message('Checking for %s command... ' % cmd)
    result = WhereIs(cmd)
    context.Result(result is not None)
    return result

def CheckHasWorkingSwig(conf):
    if (not conf.CheckCommand('python') or
        not conf.CheckCommand('swig')):
        return False
    # FIXME: add check for Python headers (Python.h)
    # see: http://www.scons.org/wiki/GSoC2008/MatiGruca/CheckPython
    return True

conf = Configure(
    base_env,
    custom_tests = {
        # Adds a custom test
        'CheckCommand': CheckCommand,
        }
    )

if CheckHasWorkingSwig(conf):
    base_env['SWIG_OPTION'] = True

# Check for C++ compiler and do sanity checks
conf.CheckCXX()

# Mandatory libraries
# fixme: is pthread mandatory for unittests ?
for lib in ('crypto',):
    # FIXME: currently these checks are not enforced some may not work
    # reliably on every supported architectures.
    conf.CheckLib(lib)

# Optional libraries
if conf.CheckLib('z'):
    base_env.Append(
        CPPDEFINES = ['HAVE_ZLIB'],
        ZLIB_OPTION = True,
    )
base_env = conf.Finish()



#### Linux environment

# Linux environnment
linux_env = base_env.Clone()
linux_env.Tool('target_platform_linux')
linux_env.Append(
    CCFLAGS = [], #['-W', '-Wall'],
    )

# Debug Linux
linux_debug_env = linux_env.Clone(
    BUILD_TYPE = 'dbg-linux',
    BUILD_TYPE_DESCRIPTION = 'Linux debug build',
    )
# Use common debug settings.
linux_debug_env.Tool('target_debug')
# Add this variant to the set which is build by default.
linux_debug_env.Append(
    BUILD_GROUPS = ['default'],
    # create the installer
    INSTALLER = installer.Installer(linux_debug_env),
    )


# Optimized Linux
linux_optimized_env = linux_env.Clone(
    BUILD_TYPE = 'opt-linux',
    BUILD_TYPE_DESCRIPTION = 'Linux optimized build',
    )
linux_optimized_env.Tool('target_optimized')
linux_optimized_env['INSTALLER'] = installer.Installer(linux_optimized_env)



#### Mac environment

# Mac environnment
mac_env = base_env.Clone()
mac_env.Tool('target_platform_mac')
mac_env.Append(
    CPPPATH = ['/opt/local/include/'],
    LIBPATH = ['/opt/local/lib'],
    )

# Debug Mac
mac_debug_env = mac_env.Clone(
    BUILD_TYPE = 'dbg-mac',
    BUILD_TYPE_DESCRIPTION = 'Mac debug build',
    )
# Use common debug settings.
mac_debug_env.Tool('target_debug')
# Add this variant to the set which is build by default.
mac_debug_env.Append(
    BUILD_GROUPS = ['default'],
    INSTALLER = installer.Installer(mac_debug_env),
    )


# Optimized Mac
mac_optimized_env = mac_env.Clone(
    BUILD_TYPE = 'opt-mac',
    BUILD_TYPE_DESCRIPTION = 'Mac optimized build',
    )
mac_optimized_env.Tool('target_optimized')
mac_optimized_env['INSTALLER'] = installer.Installer(mac_optimized_env)



#### BSD environment

# BSD environnment
bsd_env = base_env.Clone()
bsd_env.Tool('target_platform_bsd')
bsd_env.Append(
    CCFLAGS = [],
    # FIXME: is it also needed/valid on FreeBSD and NetBSD ?
    # Required on openbsd 4.5 for using unicode library ICU which is installed
    # under /usr/local.
    # See man gcc-local (openbsd):
    #   gcc does not search under /usr/local for include files nor for li-
    #   braries: as a system compiler, it only searches the system paths by
    #   default.
    CPPPATH = ['/usr/local/include/'],
    LIBPATH = ['/usr/local/lib'],
    LIBS = ['pthread'],
    )


# Debug BSD
bsd_debug_env = bsd_env.Clone(
    BUILD_TYPE = 'dbg-bsd',
    BUILD_TYPE_DESCRIPTION = 'BSD debug build',
    )
# Use common debug settings.
bsd_debug_env.Tool('target_debug')
# Add this variant to the set which is build by default.
bsd_debug_env.Append(
    BUILD_GROUPS = ['default'],
    INSTALLER = installer.Installer(bsd_debug_env),
    )


# Optimized BSD
bsd_optimized_env = bsd_env.Clone(
    BUILD_TYPE = 'opt-bsd',
    BUILD_TYPE_DESCRIPTION = 'BSD optimized build',
)
bsd_optimized_env.Tool('target_optimized')
bsd_optimized_env['INSTALLER'] = installer.Installer(bsd_optimized_env)



#### Build the variants listed above.
matched_env = BuildComponents([
    linux_debug_env,
    linux_optimized_env,
    mac_debug_env,
    mac_optimized_env,
    bsd_debug_env,
    bsd_optimized_env,
])
assert(len(matched_env) == 1)
